<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script type="text/javascript"
			src="<c:url value="/static/js/common.js"/>"></script>
<script type="text/javascript">

	function initList(listDisplayEId) {
		$('#' + listDisplayEId).datagrid(
				{
					nowrap : false,
					striped : true,
					collapsible : true,
					remoteSort : true,
					notSelectByRow : false,
					/* frozenColumns : [ [ {
						field : 'ids',
						checkbox : true
					} ] ], */
					toolbar : [
							{
								id : listDisplayEId + '_btn_edit',
								text : '提交实际到货材料重量',
								iconCls : 'icon-edit',
								handler : function() {	
									var rows = $('#'+listDisplayEId).datagrid('getRows');	
									for(var i=0; i<rows.length; i++){
									    var rowIndex=$('#'+listDisplayEId).datagrid('getRowIndex',rows[i]['ordSupMaterId']);
										$('#listDisplay').datagrid('endEdit', rowIndex);
									}
									var row = $('#'+listDisplayEId).datagrid('getRows');	

									if(row.length == 0){
										$.messager.alert('提示','请选择一条记录操作！','info');
										return;
									}else{
										var userList = new Array(); 
										var jsonFater;
										for (var i = 0 ; i < row.length;i++){
											
											/* if ( row[i]['ordMaterWeightReal'] == "" || !isNumberAndNoZero(row[i]['ordMaterWeightReal'])  ){
												alert("材料"+row[i]['ordMaterName']+"，（实际重量/个/份 ）请填写数字！");
												return false;
											} */
											var  jsonSun = "{ordMaterId:'"+row[i]['ordMaterId']+"',unusualRemark:'"+row[i]['unusualRemark']+"',ordMaterWeight:'"+row[i]['ordMaterWeight']+"',ordMaterWeightReal:'"+row[i]['ordMaterWeightReal']+"',ordMaterLevel:'"+row[i]['ordMaterLevel']+"',ordSupId:'"+row[i]['ordSupId']+"',ordId:'"+row[i]['ordId']+"',ordCustId:'"+row[i]['ordCustId']+"'}";
											userList.push(jsonSun);
										}
										jsonFater = JSON.stringify(userList);
										var str = "json="+jsonFater;	
										document.getElementsByName("materdategridList")[0].value = jsonFater;
										$.messager.confirm('提示', '是否提交记录？', function(r){
											//方法函数 进入后台
											if (r) {
												addMater();
            								}
											$('#'+listDisplayEId).datagrid('reload');
											$('#'+listDisplayEId).datagrid('clearSelections');
										});
									}
								}
							}],
					onLoadError : function() {
						$.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
					},
					onClickRow :  function(rowIndex) {
									$(".myrad").prop("checked", false);
									$(".myrad").eq(rowIndex).prop("checked", true);
									$('#listDisplay').datagrid('beginEdit', rowIndex);
					            }
					}); 

	};
	$(document).ready(function() {
		initList('listDisplay');
	});
	function addMater(){
			$('#AddFoodForm').form('submit',
			{
				url:'<c:url value='/order/commitMaterOrderReal.do'/>', //查询提交路径
				onSubmit:function()//提交按钮相应函数
				{
					return $('#AddFoodForm').form('validate');
				},
				success:function(data){
					data = eval( "("+data+")" );
	       			if(data.success){
	       				$.messager.alert("提示", "提交成功！", 'info');
						$('#listDisplay').datagrid('reload');
    				}else{
    					var tmsg= ""
    					if(data.msg == "msg005"){
    	       				$.messager.alert("提示", "状态不正确，请确认材料订单状态是否为品级确认完毕", 'warning');
    					}else if(data.msg == "msg006"){
    						$.messager.alert("提示", "今日菜品价格还没有录入，请等待或联系采购部门。", 'warning');
    					}else if(data.msg == "msg003"){
                            $.messager.alert("提示", "已经提交过实际价格。", 'warning');
                        }else if(data.msg == "msg004"){
                            $.messager.alert("提示", "订单还未发货，请确认状态为待收货。", 'warning');
                        }
    				}
				},
				error:function(){
					$.messager.alert("错误", "系统错误，请联系管理员！", 'error');
					return;
				}
			});
			
		}
		
	function formatRadio(value,rowData,rowIndex){
		return value="<input type='radio' class='myrad'  name='radioRole' />";
	}
	// 颜色
	function cellStyler(value, row, index) {
	    if(row['returnGoods']==1){
	        return 'background-color:red;';
	    }
	}
	//编辑模块
	var editIndex = undefined;
	function onClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#listDisplay').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#listDisplay').datagrid('selectRow', editIndex);
			}
		}
	}
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#listDisplay').datagrid('validateRow', editIndex)){
			var ed = $('#listDisplay').datagrid('getEditor', {index:editIndex,field:'level'});
			var levelname = $(ed.target).combobox('getText');
			$('#listDisplay').datagrid('getRows')[editIndex]['levelname'] = levelname;
			$('#listDisplay').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
</script>
</head>

<body>
    <form id="AddFoodForm" name="AddFoodForm" method="post">
		<table cellpadding="5">
			      <tr>
                    <td>
				        <input class="easyui-validatebox" type="hidden" name="ordId" required="true" validType="length[1,30]" value="${ordId }"/>
				        <input class="easyui-validatebox" type="hidden" name="materdategridList"  />
				        <input class="easyui-validatebox" type="hidden" name="ordSupId" value="${supId }" />
				        
 			        </td>
                  </tr>
        </table>
    </form>

	<table id="listDisplay" title="材料订单列表" singleSelect=false
		iconCls="icon-list" idField="ordSupMaterId"
		url="<c:url value="/order/findcustMaterDetailed.do?oid=${ordId }&supId=${supId }"/>"
		style="width: 800px; width: 1180px;" loadMsg="请稍候......"
		pagination="false" pageSize="20"
		pageList="[10,20,30,40,50,100,150]" sortName="roleId"
		rownumbers="true" sortOrder="asc">
		<thead>
				<tr>
				    <th field="id" width="25" align="center"  checkbox="false">
					</th> 
					<th field="ordSupMaterId" width="80"  sortable="true" hidden="true">
						ID
					</th>
				    <th field="ordId" width="80"  sortable="true" hidden='true' >
						订单ID
					</th>	
 					<th field="ordCustId" width="60" sortable="true" hidden ="true" hidden ='true'>
						客户ID
					</th>
					<th field="ordCustName" width="80" sortable="true" >
						客户名
					</th>
					<th field="ordSupId" width="60" sortable="true" hidden ="true" >
						供货商ID
					</th>	
					<th field="ordSupName" width="80" sortable="true" >
						供货商名
					</th>	
					<th field="ordMaterId" width="60" sortable="true" >
						材料id
					</th>
					<th field="ordMaterName" width="100"  sortable="true" >
						材料名
					</th>
					<th field="ordMaterLevel" width="50"  sortable="true"  formatter="formatFoodType" hidden="true" >
						品级
					</th>
                    <th field="brands" width="60" sortable="true" >品牌
                    </th>
                    <th field="specifications" width="60" sortable="true" >规格
                    </th>
					<th field="ordMaterWeight" width="100"  sortable="true" >
						需求材料数量
					</th>
					<th field="unit" width="60"  sortable="true" >单位
                    </th>	
					<th field="ordMaterValue" width="90"  sortable="true" hidden='true'>
						估算材料价格
					</th>
					<th field="storeWeight" width="60"  sortable="true" hidden='true'>
						材料库存
					</th>	
					<th field="ordMaterWeightReal" width="100"  sortable="true"  editor="text">
						实际到货数量
					</th>	
					<th field="ordMaterValueRealEvery" width="90"  sortable="true" >实际材料价格
                    </th>
					
					<th field="ordMaterValueReal" width="90"  sortable="true" >
						实际材料总价格
					</th>
					<th field="returnGoods" width="90"  sortable="true" formatter="formaterReturnGoods" styler="cellStyler">退货标记
                    </th>
					<th field="unusualRemark" width="150"  sortable="true" editor="text">异常说明
                    </th>
 				</tr>
		</thead>
	</table>
</body>
</html>